package tankgame5;

import java.io.*;
import java.util.Vector;

public class Recorder {
    private static int allEnemyTankNum = 0;
    private static BufferedWriter bw;

    private static BufferedReader br;
    private static String recordFile = "src\\record.txt";
    private static Vector<Enemy> enemies =null;
    private static Vector<Node> nodes = new Vector<>();//退出时坦克信息


    //读取record文件，恢复上把游戏数据
    public static Vector<Node> getNodsAndEnemyTankNum(){
        try {
            br = new BufferedReader(new FileReader(recordFile));
            allEnemyTankNum = Integer.parseInt(br.readLine());
            String data = "";
            while ((data = br.readLine())!=null){
                String[] s = data.split(" ");
                Node node = new Node(Integer.parseInt(s[0]),
                        Integer.parseInt(s[1]),
                        Integer.parseInt(s[2]));
                System.out.println(node);
                nodes.add(node);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nodes;
    }

    public static void setEnemies(Vector<Enemy> enemies) {
        Recorder.enemies = enemies;
    }

    public static void keepRecord(){
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(allEnemyTankNum+ "\n");//乱码不是数字（解决了）
//            bw.newLine();//加一个换行动作    (去掉就行)

            //保存游戏（所有敌人坦克坐标）
            for (int i = 0; i < enemies.size(); i++) {
                Enemy enemy = enemies.get(i);
                String record= enemy.getX()+" "+enemy.getY()+" "+enemy.getDirect();
                bw.write(record+"\n");//后面是加上换行
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }
    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    //击毁敌人坦克，num++
    public static  void addallEnemyTankNum(){
        Recorder.allEnemyTankNum ++;
    }

}
